var app = getApp()
var common_Js = require("../../component/common.js"),
  fetchApi = require("../../utils/httpPost.js")
import Toast from '../../component/vant/toast/toast';
const CryptoJS = require("../../utils/aesUtil.js");


Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    userInfo: {},
    hasUserInfo: false,
    current: 0,
    currentTabId: 0,
    pageIndex: 1,
    pageSize: 20,
    tabs: [], //标题tab菜单
    visibleModal: false,
    loading: false,
    netWork: true,
    isLoading: true,
    showGoAuth: true,
    hotword: '请输入商品名称',
    isLogin: 0,
    autoplay: true,
    wd: 0,
    jd: 0,
    banners: [],
    menus: [],
    partners: [],
    sections: [], //内容行集合
    productList: [],
    floorIndex: 0,
    kfPhone:'',
    newsList:[],
  },
  switchNav: function(e) {
    //0 资质服务  1 企业服务
    const index = parseInt(e.currentTarget.dataset.index);
    console.log('点击了资质和服务');

    getApp().globalData.tabBarParam = { type: index };
    wx.switchTab({
        url:'/pages/product/type/index'
    });
 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this
    var o = getCurrentPages().pop();
    console.log('onLoad o', o)
  },
  onShow: function () {
    this.data.pageIndex = 1;
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.setData({
        currentTab:0,
      isLogin: isLogin
    })
    if (isLogin == 1) {
      var user_id = app.getStorageSync("user_id");
      if (user_id > 0) {
        that.onHttpInit();
        app.getUserInfoById(user_id)
      } else {
        that.onHttpInit();
      }
    } else {
        that.onHttpInit();
    }
  },
  onProductTap(e) {
    var ooo = e.currentTarget.dataset;
    // debugger
    
    var o = e.currentTarget.dataset, i = o.item, a = i.id, r = o.positionIndex, s = e.timestamp, d = e.currentTarget.offsetTop;
    console.log('商品详情：'+e.currentTarget.dataset.item)
    if (a) {
        wx.navigateTo({
            url: '/pages/product/detail/index?id='+ a,
          })
      }


  },
  goSearch(res) {
    console.log('goSearch')
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  goNotice(res) {
    console.log('goNotice', res)
    var id = res.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/notice/detail/index?id=' + id,
    })
  },
  goShowBuild() {
    Toast('工程师正加紧建设中...');
  },
  callPlatform(){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.kfPhone,
    })
  },

  goTabbar(e) {
    var url = e.currentTarget.dataset.url;
    console.log('tabbar 跳转', url)
    wx.switchTab({
      url: url,
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/index/index', {
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex,
      longitude: that.data.jd,
      latitude: that.data.wd,
    }, function (res) {
      console.log('首页数据', res)
      if (res.data.code == 0) {
        that.data.pageIndex = that.data.pageIndex + 1;
        that.data.kfPhone = res.data.data.kfPhone;
        that.setData({
          banners: res.data.data.banners,
          menus: res.data.data.menuList,
          sections: res.data.data.sections,
          partners: res.data.data.partners,
          newsList:res.data.data.noticeList,
          productList:res.data.data.products,
        })
        that.setData({
          isLoading: false,
          loading: true,
          netWork: true,
        });
      } else {
        that.setData({
          isLoading: false,
          loading: true,
          netWork: true
        })
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function () {
      that.setData({
        isLoading: false,
        loading: true,
        netWork: true,
      })
    })
  },
  pushToDetail: function (e) {
    console.log("pushToDetail.e", e)
    var itemData = e.detail.target.dataset.item;
    console.log("itemData", itemData)
    // 链接的类型   0 - 单商品id   1 - 商品页（title_menu_id）  2 - 团购商品id 3 - 团购商品页id  4 - 优惠券id
    console.log("itemData.linkType", itemData.linkType)
    if (itemData.linkType == 'product') { //
      wx.navigateTo({
        url: '/pages/product/detail/index?id=' + itemData.linkId + '&from=appHome'
      });
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true
    })
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    // this.onHttpLoadMore()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpLoadMore: function () {
    var that = this;
    fetchApi.httpPost('/product/list', {
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
    }, function (res) {
      console.log("加载更多数据", res)
      if (res.data.totalcount > 0) {
        that.data.pageIndex = that.data.pageIndex + 1
        var products = res.data.data.products;
        if (products.length > 0) {
          for (var index in products) {
            that.data.productList.splice(that.data.productList.length, 0, products[index]);
          }
          that.setData({
            productList: that.data.productList
          });

          that.data.sections[that.data.floorIndex].products = that.data.productList
          that.setData({
            sections: that.data.sections
          })
        }
      }
      that.setData({
        isLoading: false,
        loading: true,
        netWork: true
      })
      wx.hideNavigationBarLoading() //完成停止加载
      // complete
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function () {})
  },
  goMore(e) {
    console.log('goMore', e)
    var url = e.detail.currentTarget.dataset.url;
    var type = e.detail.currentTarget.dataset.type;
    wx.navigateTo({
      url: url + "?type=" + type,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var user_id = app.getStorageSync("user_id");
    console.log("转发成功 user_id", user_id)
    return {
      title: "呈诺润鑫",
      path: '/pages/home/home?ag_id=' + user_id,
      success: function (res) {
        console.log(res, "转发成功")
      },
      fail: function (res) {
        console.log(res, "转发失败")
      }
    }
  },
})